package org.market.domain.strategy.service.rule.tree.factory;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.market.domain.strategy.model.entity.RuleActionEntity;
import org.market.domain.strategy.service.annotation.LogicStrategy;
import org.market.domain.strategy.service.rule.ILogicFilter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: Insight
 * @Description: 初始化登录工厂
 * 1.提供可以
 * @Date: 2025/2/9 上午10:19
 * @Version: 1.0
 */
@Service
public class DefaultLogicFactory {

    ConcurrentHashMap<String, ILogicFilter<?>> filterMap = new ConcurrentHashMap<>();

    public DefaultLogicFactory(List<ILogicFilter<?>> logicFilters) {
        logicFilters.forEach(logic -> {
            LogicStrategy logicStrategy = AnnotationUtils.findAnnotation(logic.getClass(), LogicStrategy.class);
            if (logicStrategy != null) {
                filterMap.put(logicStrategy.logicModel().getCode(),logic);
            }
        });
    }

    public <T extends RuleActionEntity.RaffleEntity> Map<String, ILogicFilter<T>> openLogicFilter() {
        return (Map<String, ILogicFilter<T>>)(Map<?,?>) filterMap ;
    }

    @Getter
    @AllArgsConstructor
    public enum LogicModel {
        RULE_WEIGHT("rule_weight","[抽奖前条件]根据权重可返回抽奖范围的key","before"),
        RULE_BLACKLIST("rule_blacklist","[抽奖前条件]黑名单规则过滤,根据黑名单则直接返回","before"),
        RULE_LOCK("rule_lock","[抽奖中条件]商品已满足解锁条件","during"),
        RULE_LUCK("rule_luck","[抽奖中条件]商品未满足解锁条件","after"),
        ;

        private final String code;
        private final String info;
        private final String type;

        public static boolean isDuring(String code) {
            return DefaultLogicFactory.LogicModel.valueOf(code.toUpperCase()).type . equals("during");
        }
        public static boolean isAfter(String code) {
            return DefaultLogicFactory.LogicModel.valueOf(code.toUpperCase()).type . equals("after");
        }
    }
}
